package com.hjsoft.autofu;

import com.hjsoft.autofu.utils.FontUtil;
import com.hjsoft.autofu.utils.ImgTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName:AutoFu
 * @Author:QM-LiuHongqing
 * @Create: 2023-01-17 10:45
 */
public class AutoFu extends JFrame {
    List<Color> colorList;
    List<String> arrayList;
    Integer index, fontRandom;
    BufferedImage bg;

    public AutoFu() {
        arrayList = Arrays.asList("楷体", "宋体", "微软雅黑", "华文新魏", "华文隶书", "华文楷体", "方正舒体", "华文行楷", "幼圆", "华文新魏", "华文彩云", "隶书");
        fontRandom = (int) ((Math.random() * 10) % 12);
        System.out.println("字体：" + arrayList.get(fontRandom));
        bg = ImgTool.getimg("src/main/resources/img/bg.jpg");
        //初始化
        colorList = Arrays.asList(Color.gray, Color.black, Color.PINK, Color.orange, Color.RED);
        index = (int) ((Math.random() * 10) % 5);
        System.out.println("字体颜色" + index);
        setSize(600, 600);
        setIconImage(ImgTool.getImage("src/main/resources/img/title.png"));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setBackground(Color.red);
        //键盘监听
        //设置面板键盘监听
        this.addKeyListener(new KeyAdapter() {
            //重写键盘监听方法
            @Override
            public void keyPressed(KeyEvent e) {
                //判断是否是空格
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    reGame();
                }
            }
        });

    }

    //重启
    public void reGame() {
        //关闭当前窗口
        this.dispose();
        //开启新窗口
        String[] args = {};
        main(args);
    }

    @Override
    public void paint(Graphics g) {
        //画背景
        g.drawImage(bg, 15, 50, 570, 535, null);
        FontUtil.drawWord(g, "福", colorList.get(index), 250, 163, 400, arrayList.get(fontRandom));
        //repaint();
    }

    public static void main(String[] args) {
        AutoFu autoFu = new AutoFu();
    }
}
